package l2hc.gameserver.skills.skillclasses;

import l2hc.commons.collections.GArray;

import l2hc.commons.util.Rnd;
import l2hc.gameserver.Config;
import l2hc.gameserver.ai.CtrlEvent;
import l2hc.gameserver.ai.CtrlIntention;
import l2hc.gameserver.geodata.GeoEngine;
import l2hc.gameserver.model.Creature;
import l2hc.gameserver.model.Skill;
import l2hc.gameserver.model.instances.NpcInstance;
import l2hc.gameserver.network.serverpackets.FlyToLocation;
import l2hc.gameserver.network.serverpackets.L2GameServerPacket;
import l2hc.gameserver.network.serverpackets.SystemMessage;
import l2hc.gameserver.network.serverpackets.ValidateLocation;
import l2hc.gameserver.stats.Stats;
import l2hc.gameserver.templates.StatsSet;
import l2hc.gameserver.utils.Location;
import l2hc.gameserver.utils.PositionUtils;



public class InstantJump extends Skill
{
  public InstantJump(StatsSet set)
  {
    super(set);
  }

  public void useSkill(Creature activeChar, GArray<Creature> targets)
  {
    if (targets.size() == 0) {
      return;
    }
    Creature target = targets.get(0);
    if (Rnd.chance(target.calcStat(Stats.PSKILL_EVASION, 0.0D, activeChar, this)))
    {
      if (activeChar.isPlayer())
        activeChar.sendPacket(new SystemMessage(1999).addName(target));
      if (target.isPlayer())
        target.sendPacket(new SystemMessage(2264).addName(target).addName(activeChar));
      return;
    }

    int px = target.getX();
    int py = target.getY();
    double ph = PositionUtils.convertHeadingToDegree(target.getHeading());

    ph += 180.0D;

    if (ph > 360.0D) {
      ph -= 360.0D;
    }
    ph = 3.141592653589793D * ph / 180.0D;

    int x = (int)(px + 25.0D * Math.cos(ph));
    int y = (int)(py + 25.0D * Math.sin(ph));
    int z = target.getZ();

    Location loc = new Location(x, y, z);

    if (Config.ALLOW_GEODATA) {
      loc = GeoEngine.moveCheck(activeChar.getX(), activeChar.getY(), activeChar.getZ(), x, y, activeChar.getReflection().getGeoIndex());
    }

    if (target.isNpc())
    {
      NpcInstance npc = (NpcInstance)target;
      npc.abortAttack(true, true);
      npc.abortCast(true, true);
      npc.getAI().notifyEvent(CtrlEvent.EVT_THINK);
    } else {
      target.setTarget(null);
    }
    activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
    activeChar.broadcastPacket(new L2GameServerPacket[] { new FlyToLocation(activeChar, loc, FlyToLocation.FlyType.DUMMY) });
    activeChar.abortAttack(true, true);
    activeChar.abortCast(true, true);
    activeChar.setXYZ(loc.x, loc.y, loc.z);
    activeChar.setHeading(target.getHeading());
    activeChar.broadcastPacket(new L2GameServerPacket[] { new ValidateLocation(activeChar) });
  }
}